package org.itboys.ace.web.lasted.process;


import org.itboys.admin.entity.lasted.process.SystemProcessTemplateNode;
import org.itboys.admin.service.lasted.process.SystemProcessTemplateNodeService;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *作者：江雄
 * 描述：系统-流程（审核）
 * 时间：2017年2月24日.重新构建
 * 描述：系统_流程（审核）_模板_controller
 */
@RestController
@RequestMapping("/ace/system/process/template/node")
public class AceSystemProcessTemplateNodeController extends BaseController{
    @Autowired
    private SystemProcessTemplateNodeService templateNodeService;

    @RequestMapping("/node-list")
    public ModelAndView template_list(HttpServletRequest request, HttpServletResponse response, Model model) {
        model.addAttribute("templateId",request.getParameter("templateId"));
        return new ModelAndView("/ace/sys/process/node-list");
    }

    /**
     * 分页查询流程（审核）
     * @param request
     * @param response
     */
    @RequestMapping("/select")
    public void select(HttpServletRequest request, HttpServletResponse response,
                       @RequestParam(value="templateId",required = true) Long templateId

                       ){

        PageResult<SystemProcessTemplateNode> result=templateNodeService.selectNode(request,templateId);
        JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);

    }
    /**
     * 保存
     * @param request
     * @param response
     */
    @RequestMapping(value="/save", method= RequestMethod.POST)
    public void save(@ModelAttribute SystemProcessTemplateNode templateNode,
                     HttpServletRequest request, HttpServletResponse response) {
        try{
           int resultCode= templateNodeService.saveTemplateNode(templateNode);
           if(resultCode==-2){
               AjaxUtils.renderText(response, "用户已经在本次审核模板中存在");

           }else if(resultCode==-3){
               AjaxUtils.renderText(response, "审核顺序已经存在");
           }
            AjaxUtils.renderText(response, CommonConstants.SUCCESS);
        }catch(Exception e){
            logger.error("SystemProcessTemplateNode save error", e);
            e.printStackTrace();
            AjaxUtils.renderText(response, CommonConstants.FAIL);
        }
    }
    /**
     * 查询单个
     * @param request
     * @param response
     */
    @RequestMapping(value="/findOne", method=RequestMethod.POST)
    public void findOne(@RequestParam(value="id",required = true) Long id, HttpServletRequest request, HttpServletResponse response) {
        try{
            AjaxUtils.renderJson(response,templateNodeService.findOne(id));
        }catch(Exception e){
            logger.error("SystemProcessTemplate findOne error", e);
            e.printStackTrace();
            AjaxUtils.renderText(response, CommonConstants.FAIL);
        }
    }
    /**
     * 删除
     * @param id
     * @param response
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public void delete(@RequestParam(value="id",required = true) Long id, HttpServletResponse response){
        try {
            templateNodeService.delete(id);
            AjaxUtils.renderText(response, CommonConstants.SUCCESS);
        } catch (Exception e) {
            logger.error("SystemProcessTemplateNode delete error", e);
            AjaxUtils.renderText(response, CommonConstants.FAIL);
        }
    }



}
